# -*- coding: utf-8 -*-
# @Time : 2022/3/24 17:01

from src.ApiTest.ProjectConfig.Database.sprint_database import Sprint, SprintSchema
from src.User.Database.user_database import User, UserSchema
from Common.yaml_method import YamlMethod
import ast
from sqlalchemy import and_


class SprintList:
    """
    获取项目迭代列表接口
    """

    @staticmethod
    def sprint_list(page, limit, sprint_name, user_id, project_id):
        """
        获取迭代列表接口
        :param page: 页码
        :param limit: 每页多少条数据
        :param sprint_name: 迭代名称
        :param user_id: user_id
        :param project_id: 项目ID
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        if project_id:
            if sprint_name:
                # projectId和sprintName都不为空
                data = Sprint.query.filter_by(sprintName=sprint_name, projectId=project_id).\
                    paginate(page=page, per_page=limit)
            else:
                # projectId不为空，sprintName都为空
                data = Sprint.query.filter_by(projectId=project_id).paginate(page=page, per_page=limit)
        else:
            # 先查询用户所属项目信息
            user = User.query.filter_by(id=user_id).first()
            user_schema = UserSchema()
            user_data = user_schema.dump(user)
            user_project_id = user_data['user_projects']
            id_list = ast.literal_eval(user_project_id)
            # 开始构建过滤条件
            filters = [Sprint.projectId.in_(id_list)]
            if sprint_name:
                # projectId为空，sprintName不为空
                filters.append(Sprint.sprintName == sprint_name)
                data = Sprint.query.filter(and_(*filters)).paginate(page=page, per_page=limit)
            else:
                # projectId和sprintName都为空
                data = Sprint.query.filter(Sprint.projectId.in_(id_list)).paginate(page=page, per_page=limit)

        info = []
        if data:
            for i in data.items:
                mysql_schema = SprintSchema()
                mysql_data = mysql_schema.dump(i)
                # 将单条header信息添加到info中
                info.append(mysql_data)

        total = data.total
        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'sprintList': info,
                'total': total
            }
        }

        return res

    @staticmethod
    def sprint_name_list(project_id):
        """
        获取项目迭代名称列表接口
        :param project_id: 项目ID
        """
        code = YamlMethod().read_data('code.yaml')['code']
        data = Sprint.query.filter_by(projectId=project_id).all()
        info = []
        for i in data:
            mysql_schema = SprintSchema()
            mysql_data = mysql_schema.dump(i)
            data = {
                'sprintName': mysql_data['sprintName']
            }
            # 将单条sprint_name信息添加到info中
            info.insert(0, data)

        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'sprintNameList': info,
            }
        }

        return res
